﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Dapper;
using WebApplication1.Models;
using MySql.Data.MySqlClient;

namespace WebApplication1.Controllers
{
    [Route("api/[controller]/action")]
    [ApiController]
    public class DapperTestController : ControllerBase
    {
        //MySqlConnection con = new MySqlConnection("database=student;data source=43.138.224.67;user id=root;password=ww623025;");
       private readonly MySqlConnection dbcontext;
        public DapperTestController() {
            dbcontext = new MySqlConnection("database=student;data source=43.138.224.67;user id=root;password=ww623025;");
        }
        [HttpGet]
        public object Get() {

            //con.Open();
           //多条数据查询
            //var list = dbcontext.Query($"select * from userinfo ;");
            var list = dbcontext.Query<userinfo>($"select * from userinfo ;").ToList();
            //返回单挑信息
            var user = dbcontext.Query<userinfo>($"select * from userinfo where userid = {132};").FirstOrDefault();
            return list;
        
        }

        [HttpGet("insert")]
        public void insert()
        {
            //单挑数据插入
          //  var a = dbcontext.Execute($"insert into userinfo(userid,username,sex,gangweiId,lingdaoid) VALUES({132},'{"吊袜带"}','{'男'}',{1},{2});");
            //多条插入

            var itemsToInsert = new List<userinfo>{
        new userinfo { userid =133, username="消亡",sex="男",gangweiId=1,lingdaoid=2},
         new userinfo { userid =134, username="d消亡",sex="男",gangweiId=1,lingdaoid = 2},
         new userinfo { userid =135, username="c消亡",sex="男",gangweiId=1,lingdaoid = 2},
    };

            dbcontext.Execute("insert into userinfo(userid,username,sex,gangweiId,lingdaoid) VALUES(@userid,@username,@sex,@gangweiId,@lingdaoid);", itemsToInsert);


        }


        //public object getlist() {

        //    dbcontext.Query<userinfo>($"select * from userinfo where userid In (@ids);"new { ids= new int[] {1,2,3 } });
        //}

    }
   


}
